﻿#include "mylabel.h"

MyLabel::MyLabel(QWidget* parent):QLabel(parent)
{
    zoom = 0;
    // isEnlarge = false;
    // isHover = true;
    isLeftMousePressMove = false;
    setMouseTracking(true);
}

MyLabel::~MyLabel()
{

}

// void MyLabel::setEnlargeEnable(bool en)
// {
//     //    setAttribute(Qt::WA_Moved,true);
//     isEnlarge = en;
//     setMouseTracking(isHover || isEnlarge);
// }

// void MyLabel::setHoverEnable(bool en)
// {
//    setAttribute(Qt::WA_Moved,true);
//     isHover = en;
//     setMouseTracking(isHover);
// }

Qt::CursorShape MyLabel::getCursor(int pos)
{
    switch (pos) {
        case 0:
            return Qt::SizeFDiagCursor;
        case 1:
            return Qt::SizeVerCursor;
        case 2:
            return Qt::SizeBDiagCursor;
        case 3:
            return Qt::SizeHorCursor;
        case 4:
            return Qt::SizeFDiagCursor;
        case 5:
            return Qt::SizeVerCursor;
        case 6:
            return Qt::SizeBDiagCursor;
        case 7:
            return Qt::SizeHorCursor;
        case 8:
            return Qt::OpenHandCursor;
        case 9:
            return Qt::ClosedHandCursor;
        default:
            break;
    }
    return Qt::ArrowCursor;
}


//void MyLabel::paintEvent(QPaintEvent *event){
//    QPainter painter(this);
//    //判断是否被遮盖

//}

void MyLabel::mouseDoubleClickEvent(QMouseEvent *event){
    Q_UNUSED(event);
    // if(isEnlarge){
    //     retInfo("mouseDoubleClickEvent");
    //     emit imageDoubleClicked();
    // }
}

void MyLabel::mousePressEvent(QMouseEvent *ev)
{
    if(zoom>0){
        //移动
        setCursor(Qt::OpenHandCursor);
        if(ev->buttons() & Qt::LeftButton){
            if(!isLeftMousePressMove){
                retInfo(QString("mouse press left (%0,%1)").arg(ev->pos().x()).arg(ev->pos().y()));
                isLeftMousePressMove = true;
            }
            curPoint = ev->pos();
        }
    }
}

void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
    Q_UNUSED(ev);

    // if(zoom>0){
        //停止移动
        setCursor(Qt::ArrowCursor);
        isLeftMousePressMove = false;
    // }
}

bool MyLabel::isPointRect(const QPoint p,const QRect R)
{
    bool b = R.contains(p);
//    retInfo(QString("(%0,%1) in (%2,%3,%4,%5) is %6").arg(p.x()).arg(p.y())
//                .arg(R.x()).arg(R.y()).arg(R.width()).arg(R.height()).arg(b));
    return b;
}

void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    if(isLeftMousePressMove){
        retInfo(QString("cur mouse move[%0,%1] %2").arg(event->pos().x()).arg(event->pos().y()).arg(isLeftMousePressMove));
        emit imageMove(event->pos().x()-curPoint.x(),event->pos().y()-curPoint.y());
        curPoint = event->pos();
    }
}

void MyLabel::wheelEvent(QWheelEvent *ev){
    QPoint degrees = ev->angleDelta();
    // retInfo(QString("mylabel mouse wheel x=%0,y=%1 pos x=%2 y=%3").arg(degrees.x()).arg(degrees.y()).arg(ev->pos().x()).arg(ev->pos().y()));
    emit imageZoom(degrees.y(),ev->pos().x(),ev->pos().y());
}
